package com.flashidea.zddziot.activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;

import com.flashidea.zddziot.ContentValues;
import com.flashidea.zddziot.R;
import com.flashidea.zddziot.utlis.BluetoothChatService;
import com.flashidea.zddziot.utlis.CMDUtils;
import com.flashidea.zddziot.view.ColorPickerView;
import com.flashidea.zddziot.view.ColorPickerView.OnColorChangedListener;

public class LightColorActivity extends BluetoothChatActivity implements OnClickListener {
	
	protected static final String TAG = "LightColorActivity";
	protected static final boolean D = ContentValues.D;
	private RelativeLayout colorContent;
	//亮度//0 - 100
	private int currA;
	//红//0 - 255
	private int currR;
	//绿//0 - 255
	private int currG;
	//蓝//0 - 255
	private int currB;
	private SharedPreferences sp;
	private ColorPickerView colorPicker;
	
	private long time;
	long currentTime;
	private SeekBar sb;
	private TextView tv_light;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.setContentView(R.layout.activity_light_color);
		
		sp = getSharedPreferences(ContentValues.PREF_FILE_NAME, Context.MODE_PRIVATE);
		currA = sp.getInt("currA", 80);  //默认80
		currR = sp.getInt("currR", 0);  //默认125
		currG = sp.getInt("currG", 0);  //默认125
		currB = sp.getInt("currB", 0);  //默认125
		
	}
	
	@Override
	protected void initView() {
		colorContent = (RelativeLayout) findViewById(R.id.rl_color_content);
		colorPicker = getColorView();
		colorContent.addView(colorPicker);
		findViewById(R.id.tv_light_title).setOnClickListener(this);
		findViewById(R.id.ib_color_red).setOnClickListener(this);
		findViewById(R.id.ib_color_gre).setOnClickListener(this);
		findViewById(R.id.ib_color_blu).setOnClickListener(this);
		findViewById(R.id.ib_color_whi).setOnClickListener(this);
		findViewById(R.id.ib_color_yel).setOnClickListener(this);
		findViewById(R.id.ib_color_whi_yel).setOnClickListener(this);
		sb = (SeekBar)findViewById(R.id.sb_light_intensity);
		tv_light = (TextView) findViewById(R.id.tv_light_intensity);
		tv_light.setText(String.valueOf(currA));
		sb.setProgress(currA);
		sb.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if(MotionEvent.ACTION_UP == event.getAction() ){
					if(D) Log.i(TAG, "SeekBar onTouch...");
					currentTime = System.currentTimeMillis();
					time = currentTime;
					currA = sb.getProgress();  //0 - 100
					tv_light.setText(String.valueOf(currA));
					//发送命令
					sendColor();
				}
				return false;
			}
		});
		sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			
			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
			}
			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				
			}
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				tv_light.setText(String.valueOf(progress));
				currentTime = System.currentTimeMillis();
				if(currentTime - time < 500){  //半秒发一次
					return;
				}
				time = currentTime;
				currA = progress;  //0 - 100
				//发送命令
				sendColor();
			}
		});;
	}
	private synchronized void sendColor() {
		int a = (int)((currA / 100.0f) * 255);
		String light = CMDUtils.getLight(currR, currG, currB, a);
		if(D){
//			Toast.makeText(this, light, 0).show();
			Log.i(TAG, "" + currR+ "," + currG+ "," + currB+ "," + a);
			Log.i(TAG, light);
		}
		sendMessage(light);
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.tv_light_title :
				finish();
				break;
			case R.id.ib_color_red :
				currR = 255;
				currG = 0;
				currB = 0;
				sendColor();
				colorPicker.setCurrColor(Color.rgb(currR, currG, currB));
				break;
			case R.id.ib_color_gre :
				currR = 40;
				currG = 255;
				currB = 40;
				sendColor();
				colorPicker.setCurrColor(Color.rgb(currR, currG, currB));
				break;
			case R.id.ib_color_blu :
				currR = 0;
				currG = 0;
				currB = 255;
				sendColor();
				colorPicker.setCurrColor(Color.rgb(currR, currG, currB));
				break;
			case R.id.ib_color_whi :
				currR = 255;
				currG = 255;
				currB = 255;
				sendColor();
				colorPicker.setCurrColor(Color.rgb(currR, currG, currB));
				break;
			case R.id.ib_color_yel :
				currR = 242;
				currG = 246;
				currB = 0;
				sendColor();
				colorPicker.setCurrColor(Color.rgb(currR, currG, currB));
				break;
			case R.id.ib_color_whi_yel :
				currR = 255;
				currG = 250;
				currB = 156;
				sendColor();
				colorPicker.setCurrColor(Color.rgb(currR, currG, currB));
										
				break;
			default :
				break;
		}
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		Editor edit = getSharedPreferences(ContentValues.PREF_FILE_NAME, Context.MODE_PRIVATE).edit();
		edit.putInt("currA", currA);
		edit.putInt("currR", currR);
		edit.putInt("currG", currG);
		edit.putInt("currB", currB);
		edit.commit();
	}
	
	private ColorPickerView getColorView() {
		WindowManager manager = getWindow().getWindowManager();
		int height = (int) (manager.getDefaultDisplay().getHeight());
		int width = (int) (manager.getDefaultDisplay().getWidth());
		ColorPickerView myView = new ColorPickerView(this, height, width,Color.rgb(currR, currG, currB));
		myView.setOnColorChangedListener(new OnColorChangedListener() {
			@Override
			public void colorChanged(int coor) {
				currR = Color.red(coor);
				currG = Color.green(coor);
				currB = Color.blue(coor);
				
				sendColor();
			}
		});
		return myView;
	}
	

	@Override
	protected void onAfterWriteMessage(String writeMessage) {
		
	}

	@Override
	protected void onConnectedDeviceNameChange(String mConnectedDeviceName2) {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void onReadMessage(String readMessage) {
		if(D) Log.e(TAG, "onReadMessage " + readMessage);
		if(readMessage.startsWith(CMDUtils.LIGHT) && readMessage.length() > 16){
			readRGB(readMessage);
		}
	}
	
	private void readRGB(String readMessage){ //AT#12255255255255
		if(D) Log.e(TAG, "readRGB " + readMessage);
		try {
			currR = Integer.valueOf(readMessage.substring(5, 8));
			currG = Integer.valueOf(readMessage.substring(8, 11));
			currB = Integer.valueOf(readMessage.substring(11, 14));
			currA = (int)((Integer.valueOf(readMessage.substring(14, 17)) / 255.0f) * 100);
			colorPicker.setCurrColor(Color.rgb(currR, currG, currB));
			sb.setProgress(currA);
		} catch (NumberFormatException e) {
			Log.e(TAG, "颜色格式异常" + e);
			if(D) Toast.makeText(this, "颜色异常" + readMessage, 0).show();
		}
		
	}

	
	

}